﻿using Exam.infranstuctrue.Dto.Request.Task;
using Exam.infranstuctrue.Tools;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Exam.Controllers
{
    [ApiController]
    public class TaskController : BaseController
    {
        private readonly ITaskServer _taskServer;
        private readonly Session _session;

        public TaskController(ITaskServer taskServer, Session session)
        {
            _taskServer = taskServer;
            _session = session;
        }
        /// <summary>
        /// 获取任务列表
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTask(int page = 1, int limit = 10, string? key = "")
        {
            key = key ?? "";
            var token = _session.GetToken();
            var result = await _taskServer.GetTask(token, page, limit, key);
            return Ok(result);
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddTask(TaskDto dto)
        {
            dto.token = _session.GetToken();
            var result = await _taskServer.AddTask(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> UpdateEchoTask(string tid)
        {
            var token = _session.GetToken();
            var result = await _taskServer.UpdateEchoTask(tid, token);
            return Ok(result);
        }
        /// <summary>
        /// 修改任务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateTask(TaskDto dto)
        {
            dto.token = _session.GetToken();
            var result = await _taskServer.UpdateTask(dto);
            return Ok(result);
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DeleteTask(string tid)
        {
            var token = _session.GetToken();
            var result = await _taskServer.DeleteTask(tid, token);
            return Ok(result);
        }
        /// <summary>
        /// 统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> TaskPaper()
        {
            var result = await _taskServer.TaskPaper();
            return Ok(result);
        }
        /// <summary>
        /// 难度统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> QuestionLv()
        {
            var result = await _taskServer.QuestionLv();
            return Ok(result);
        }
    }
}
